const Actions = require('./actions');
const DB = require('app/db.js');
import { Storage } from 'clientConfig/util/StoreData'
const device = require('clientConfig/util/jsapi/device');
import { ready } from 'clientConfig/util/queryurlfield'
import { Control } from 'react-keeper';
import { Toast } from 'saltui';
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {

    },
    //获取打分列表
    onFetch: function (params, cb) {
        let t = this;
        if (!params.placeId) return;
        DB.Teacher.edit_item_score({
            schoolId: Storage.get('user.schoolId'),
            scoreId: params.scoreId,
            placeId: params.placeId,
            entranceFlag: "edit"
        })
            .then((content) => {
                if (content && content.code == 1) {
                    for (let [key, item] of Object.entries(content.data)) {
                        t.data[key] = item;
                    }
                    t.data["content"] = content.data["remark"];
                    t.data["wordcount"] = content.data["remark"].length;
                    t.updateComponent();
                } else {
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }

            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    //提交
    onSubmit: function (params, cb) {
        let t = this;
        // ready(() => { device.showPreloader() })
        DB.Teacher.save_sanitation_score({
            schoolId: Storage.get('user.schoolId'),
            scoreId: params.scoreId,//''新增
            placeId: params.placeId,
            timeId: params.timeId,
            typeId: params.typeId,
            data: params.data,
            remark: params.remark,
            teacherId: Storage.get('user.teacherId')
        })
            .then((content) => {

                if (content && content.code == 1) {
                    if ((!params.noimg && params.urlList && params.urlList.length > 0) || params.delscoreImgIds) {
                        return { schoolId: content.check.schoolId, delSanitationImgIds: params.delscoreImgIds, scoreId: params.scoreId, formData: params.formData }
                    } else {
                        ready(() => { device.hidePreloader() })
                        Toast.show({
                            type: 'success',
                            content: content.msg=='success'?'发布成功':content.msg
                        });
                        Control.go("/home/addgrade/" + "original")
                    }

                } else {
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            }).then((params) => {
                if (!params) return;
                var url = "/picture/uploadPicturesSanitation?schoolId=" + params.schoolId + "&scoreId=" + params.scoreId + "&delSanitationImgIds=" + params.delSanitationImgIds;
                fetch(url, {
                    method: "POST",
                    body: params.formData
                }).then(function (response) {
                    return response.json();
                }).then(function (result) {
                    // ready(() => { device.hidePreloader() })
                    if (result.content && result.content.code == 1) {
                        Toast.show({
                            type: 'success',
                            content: result.content.msg=='success'?'发布成功':result.content.msg,
                            hasMask: true,
                            duration: 2000,
                        });
                        t.data = {}//清空数据
                        Control.go("/home/addgrade/" + "original")
                    }
                }, function (error) {
                    Toast.hide(() => {
                    });
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                })
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
